# -*- coding: utf-8 -*-

import csv
import urllib2
import tweepy
from passwd import powerhoge as p

consumer_key        = p.consumer_key
consumer_secret     = p.consumer_secret
access_token        = p.access_token
access_token_secret = p.access_token_secret

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth_handler=auth)

result = [ '0', '0', '0', '0' ]

try:
    f = urllib2.urlopen( 'http://www.tepco.co.jp/forecast/html/images/juyo-j.csv' )
except urllib2.HTTPError, e:
    exit( )

f.readline( )
f.readline( )
max = f.readline( ).split( ',' )
f.readline( )
f.readline( )
f.readline( )
f.readline( )
f.readline( )

reader = csv.reader( f )
for row in reader:
    if len( row ) == 0:
        break
    if row[2] != '0':
        result = row

buffer = result[ 0 ] + ' ' + result[ 1 ] + ' ' + str( int( float( result[ 2 ] ) / float( max[ 0 ] ) * 100 ) ) + '% (' + result[ 2 ] + '/' + max[ 0 ] + ')'

api.update_status(buffer)

